import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;


public class StartupScreen extends JFrame{

	Game game;
	JButton enter;
	JLabel title;
	boolean end;
	public StartupScreen(Game game)
	{
		this.game = game;
		enter = new JButton("ENTER");
		enter.setBackground(Color.GREEN);
		MyPanel panel = new MyPanel();
		end = true;
		enter.addActionListener(new Listener());
		String path = getClass().getClassLoader().getResource(".").getPath();
		File file = new File(path + "\\Sprites\\Start_Screen.jpg");
		BufferedImage img = null;
		try {
			img = ImageIO.read(file);
			panel.setImg(img);
		} catch (IOException e) {
			System.out.printf(e.toString());
		}
		panel.add(enter,BorderLayout.SOUTH);
		this.setSize(new Dimension(600,600));
		this.add(panel);
		this.setVisible(true);
		this.addWindowListener( new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent we) {
            	try{
            	GameSettings.getDBComm().close();
            	}
            	catch(Exception e)
            	{
            		
            	}
            	if(end)
                System.exit(0);
            }
        } );
	}
	private void closeWindow()
	{
		WindowEvent wev = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
		Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
	}
	private class Listener implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0) {
			end = false;
			new CharacterSelect(game);
			closeWindow();
			
		}
		
	}
	public class MyPanel extends JPanel {
		BufferedImage img;
		public MyPanel()
		{
			super(new BorderLayout());
		}
		public void setImg(BufferedImage img)
		{
			this.img = img;
		}
		  public void paintComponent(Graphics g) {
		    super.paintComponent(g);
		    Graphics2D g2 = (Graphics2D) g;
		    g2.drawImage(img.getScaledInstance(this.getWidth(),this.getHeight(), BufferedImage.SCALE_DEFAULT), 0, 0, null);
		  }
		}
	
}
